{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable\n",
    "import torch.nn.functional as F\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = torch.unsqueeze(torch.linspace(-1 , 1, 100), dim=1)\n",
    "y = x.pow(2)+0.2*torch.rand(x.size())\n",
    "x, y = Variable(x), Variable(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAcLUlEQVR4nO3df6xkZX3H8feXy0UvlXJBVmUvXKEJrl2DsnhFKqbi2rosxrJiG0CrlppsaKQpJCVe0lRJ+IO1xKqNKNlSojaNbFMoUlndWtGQrMGyyPJjQXAFlb1LBZSrtXujd+HbP2YGzj17zsxzZs45c358XsnN3pk5M/PszJnvfeb7fJ/nMXdHRETq77BxN0BERPKhgC4i0hAK6CIiDaGALiLSEAroIiINcfi4nvi4447zk046aVxPLyJSS/fcc88z7r4q6baxBfSTTjqJXbt2jevpRURqycx+nHabUi4iIg2hgC4i0hAK6CIiDaGALiLSEAroIiINMbYql2Hceu8C1+54hP2LS6yenuKKDWvYtG5m3M0SEamE2gT0W+9d4MpbHmBp+TkAFhaXuPKWBwAU1EVEqFFAv3bHIy8E856l5ee4dscjCugiUgtFZxlqE9D3Ly5lul5EpErKyDLUZlB09fRUputFRKqkX5YhL7UJ6FdsWMPU5MSK66YmJ7hiw5oxtUhEJFwZWYbaBPRN62a45vxTmZmew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Hgect/C2mVms8AtwAfc/dHI9f3e0zLa9aru+4eZnUEnpvws5L5FtqvbnqOBtxE55wp+vUIUe34VMdKbxw+dD+8+4NfAT4Ed3etXA9sjx51Lpyrih3RSNb3rXw58E/hB999jc2pX4uMmtOtIOif20bH7/zPwAHB/9w07vqx20RlBv6/7s6cqrxed9IF3X5Pd3Z9zi3i9ks4X4BLgku7vBlzXvf0BIhVWaedaTq/ToHbdADwbeX12DXpPS2rXpd3nvY/OYO1bqvB6dS//GXBT7H5Fv15fBp4ElunErw+XeX5p6r+ISEPUPeUiIiJdCugiIg2hgC4i0hAK6CIiDaGALiLSEAroIiINoYAuItIQ/w//Hsa/iiW5agAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x.data.numpy(), y.data.numpy())\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(torch.nn.Module):\n",
    "    def __init__(self, n_feature, n_hidden, n_output):\n",
    "        super().__init__()\n",
    "        self.hidden = torch.nn.Linear(n_feature, n_hidden)\n",
    "        self.predict = torch.nn.Linear(n_hidden, n_output)\n",
    "    \n",
    "    def forward(self, x):\n",
    "        x = F.relu(self.hidden(x))\n",
    "        x = self.predict(x)\n",
    "        return x\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Net(\n",
      "  (hidden): Linear(in_features=1, out_features=10, bias=True)\n",
      "  (predict): Linear(in_features=10, out_features=1, bias=True)\n",
      ") Sequential(\n",
      "  (0): Linear(in_features=1, out_features=10, bias=True)\n",
      "  (1): ReLU()\n",
      "  (2): Linear(in_features=10, out_features=1, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "net1 = Net(1, 10, 1)\n",
    "net2 = torch.nn.Sequential(\n",
    "    torch.nn.Linear(1, 10),\n",
    "    torch.nn.ReLU(),\n",
    "    torch.nn.Linear(10, 1),\n",
    ")\n",
    "print(net1, net2)\n",
    "net = net2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "optimizer = torch.optim.SGD(net.parameters(), lr=0.1)\n",
    "loss_func = torch.nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.0286, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0390, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0363, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0241, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0119, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0145, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0179, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0283, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0154, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0147, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0217, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0288, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0215, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0218, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0210, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0084, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0172, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0273, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0282, grad_fn=<MseLossBackward>)\n",
      "tensor(0.0208, grad_fn=<MseLossBackward>)\n"
     ]
    }
   ],
   "source": [
    "for t in range(100):\n",
    "    prediction = net(x)\n",
    "    \n",
    "    loss = loss_func(prediction, y)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    \n",
    "    if t%5 == 0:\n",
    "        print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.save(net, \"net.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.2169399 ],\n",
       "       [1.1835179 ],\n",
       "       [1.1500959 ],\n",
       "       [1.1166741 ],\n",
       "       [1.0832521 ],\n",
       "       [1.0498302 ],\n",
       "       [1.0164082 ],\n",
       "       [0.9829862 ],\n",
       "       [0.94956434],\n",
       "       [0.9161424 ],\n",
       "       [0.8827204 ],\n",
       "       [0.8492985 ],\n",
       "       [0.8158765 ],\n",
       "       [0.78245455],\n",
       "       [0.7490326 ],\n",
       "       [0.7156107 ],\n",
       "       [0.6821887 ],\n",
       "       [0.64876676],\n",
       "       [0.6105158 ],\n",
       "       [0.570229  ],\n",
       "       [0.52994215],\n",
       "       [0.48965532],\n",
       "       [0.44936854],\n",
       "       [0.4090817 ],\n",
       "       [0.3687948 ],\n",
       "       [0.32850802],\n",
       "       [0.28822118],\n",
       "       [0.2479344 ],\n",
       "       [0.20764753],\n",
       "       [0.16736072],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.153136  ],\n",
       "       [0.17621925],\n",
       "       [0.21763709],\n",
       "       [0.259055  ],\n",
       "       [0.3004728 ],\n",
       "       [0.34189063],\n",
       "       [0.38330847],\n",
       "       [0.42472637],\n",
       "       [0.46614426],\n",
       "       [0.50756204],\n",
       "       [0.54897994],\n",
       "       [0.59039783],\n",
       "       [0.6318156 ],\n",
       "       [0.67323357],\n",
       "       [0.7146513 ],\n",
       "       [0.75606924],\n",
       "       [0.7974871 ],\n",
       "       [0.83890486],\n",
       "       [0.88032275],\n",
       "       [0.92174065],\n",
       "       [0.9631584 ],\n",
       "       [1.0045763 ],\n",
       "       [1.0459942 ],\n",
       "       [1.087412  ],\n",
       "       [1.12883   ],\n",
       "       [1.1702478 ],\n",
       "       [1.2116656 ],\n",
       "       [1.2530835 ],\n",
       "       [1.2945014 ],\n",
       "       [1.3359191 ],\n",
       "       [1.377337  ]], dtype=float32)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.load(\"net.pkl\")(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.7 64-bit",
   "language": "python",
   "name": "python37764bita7c2719225b84763be647c75e40e67b2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
